package com.lanqiao.controller;

import cn.hutool.captcha.CircleCaptcha;
import com.lanqiao.dao.userDAO;
import com.lanqiao.entity.userinfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/login", loadOnStartup = 1)
public class loginController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method.equals("login")) {
            //实现登录功能
            login(req, resp);
        } else if (method.equals("tologin")) {
            //跳转到登录页面
            tologin(req, resp);
        } else if (method.equals("exit")) {
            exit(req, resp);
        }
    }

    //实现跳转到登录页面  在浏览器后输入/user?method=tologin
    public void tologin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("login.jsp").forward(req, resp);
    }

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String email = req.getParameter("email");
        String password = req.getParameter("password");

        String captcha = req.getParameter("captcha");
        try {
            CircleCaptcha sessionCaptcha = (CircleCaptcha) req.getSession().getAttribute("captcha");
            if (sessionCaptcha.verify(captcha)) {
                //获取登录的邮箱和密码
                userDAO user = new userDAO();
                List<userinfo> userinfos = user.select(email, password);

                if (userinfos.size() == 0) {
                    //邮箱·密码不存在
                    resp.getWriter().println(
                            "<script>alert('账号或密码不正确，请重新输入');location.href='user?method=tologin'</script>"
                    );
                } else {
                    //保存登录状态
                    //1、将登录的信息保存在session
                    req.getSession().setAttribute("user", userinfos.get(0).getUser_ID());
                    //2、将登录信息保存到cookie
                    Cookie cookie = new Cookie("user_ID", "" + userinfos.get(0).getUser_ID());
                    //设置cookie最大保存时间
                    cookie.setMaxAge(60 * 60 * 24 * 7);
                    //将cookie的路径设置为 / 保证所有路径都能够访问
                    cookie.setPath("/");
                    resp.addCookie(cookie);

                    //账号密码存在
                    resp.getWriter().println(
                            "<script>alert('登录成功！');location.href='index'</script>"
                    );
                }
            } else {
                resp.getWriter().println(
                        "<script>alert('验证码错误，请重新输入');location.href='user?method=tologin'</script>"
                );
            }
        } catch (NullPointerException e) {

        }
    }

    //实现安全退出
    public void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //销毁session
        req.getSession().invalidate();
        //销毁cookie
        Cookie cookie = new Cookie("user_ID", "");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        resp.addCookie(cookie);

        resp.getWriter().println(
                "<script>alert('安全退出！');location.href='user?method=tologin'</script>"
        );
    }

}
